// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router.js'
import "./assets/iconfont.css"
import "./assets/common.js"
import "./assets/mui/css/mui.css"
import "./assets/base.css"
import mui from "./assets/mui/js/mui.min.js"
import axios from "axios"
import Mintui from "../node_modules/mint-ui"
import "mint-ui/lib/style.css"
import "./assets/mui/css/icons-extra.css"
import Vuex from "vuex"
import { start } from 'repl';
Vue.use(Vuex)
Vue.use(Mintui);
// import "./assets/mui/js/mui-slider.js"

Vue.config.productionTip = false
Vue.prototype.mui = mui
Vue.prototype.$axios = axios
// var car = JSON.parse(localStorage.getItem("car")||"[]")
// let car = []
var car = JSON.parse(localStorage.getItem("car")||"[]")

var store = new Vuex.Store({
  state:{
      car
  },
  mutations:{
    addToCar(state,goodsInfo){
      //点击加入购物车，把商品信息保存到store中的car上
      var flag = false
      state.car.some(item => {
        if(item.id == goodsInfo.id){
          item.count += parseInt(goodsInfo.count)//将字符串转换为数字
          flag = true
          return true
        }
      })
      if(!flag){
        state.car.push(goodsInfo)
        
      }

      var value = JSON.stringify(state.car)
      localStorage.setItem('car', value)
    },
    //把car 存到localstorage里 
    removeGood(state,id){
      state.car.some((item,i)=>{
        if(item.id == id){
           state.car.splice(i,1)
           return true
        }
      })
      var value = JSON.stringify(state.car)
      localStorage.setItem('car', value)
    },
  },
  getters:{
    
    addcount(state){
      var str = 0
      for(var c = 0; c<state.car.length;c++){
        str = str + state.car[c].count
      }
      return str 
    },
    getGoodsCount(state){
      var o = {}
      state.car.forEach(item=>{
        o[item.id] = item.count
      })
      // console.log(o)
      return o
    }
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
